package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.membershipcard;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.OilCardAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.util.ThrottleFirstUtil;
import com.zzsy.carosshopprojects.view.GridDividerItemDecoration;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

import static android.content.Context.INPUT_METHOD_SERVICE;

/**
 * 会员开卡
 */
public class MemberShipCardFragment extends BaseFragment {
    public static MemberShipCardFragment newInstance(String uuid) {
        MemberShipCardFragment memberShipCardFragment = new MemberShipCardFragment();
        Bundle bundle = new Bundle();
        bundle.putString("uuid", uuid);
        memberShipCardFragment.setArguments(bundle);
        return memberShipCardFragment;
    }

    @BindView(R.id.tv_card_suf)
    TextView tvCardSuf;
    @BindView(R.id.tv_card)
    TextView tvCard;
    @BindView(R.id.et_user_name)
    EditText etUserName;// 获取输入的用户名
    @BindView(R.id.et_user_phone)
    EditText etUserPhone;  //获取输入的电话号
    @BindView(R.id.et_psd_one)
    EditText etPsdOne; //获取输入的密码
    @BindView(R.id.et_psd_two)
    EditText etPsdTwo; //获取再次输入的数据
    @BindView(R.id.tv_store_name)
    TextView tvStoreName;//设置门店名称
    @BindView(R.id.ll_is_show)
    LinearLayout llIsShow;
    @BindView(R.id.cb_fa_box)
    CheckBox cbBoxPsd;
    @BindView(R.id.recycleView)
    RecyclerView recyclerView;
    private String paySwitch = "0"; // 0 关闭 1 开启
    private OilCardAdapter oilCardAdapter;
    private MemberShipCardMode memberShipCardMode;
    private String cardKindId = "";
    private String productId;
    private String carNumber;

    @Override
    protected int initLayout() {
        return R.layout.fg_membershipcard;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.MemberShipCard), getActivity());
        memberShipCardMode = getViewModelProvider().get(MemberShipCardMode.class);
        oilCardAdapter = new OilCardAdapter(getContext(), null, 0);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        int space = ScreenAdapterTools.getInstance().loadCustomAttrValue(20);
        GridDividerItemDecoration gridDividerItemDecoration = new GridDividerItemDecoration(getContext(), space);
        recyclerView.addItemDecoration(gridDividerItemDecoration);
        recyclerView.setAdapter(oilCardAdapter);
        oilCardAdapter.setOnItemClickListener((v, position) -> {
            oilCardAdapter.setPos(position);
            productId = oilCardAdapter.getListData().get(position).getProductId();
            cardKindId = oilCardAdapter.getListData().get(position).getId();
        });

        cbBoxPsd.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                llIsShow.setVisibility(View.VISIBLE);
                paySwitch = "1";
            } else {
                llIsShow.setVisibility(View.GONE);
                paySwitch = "0";
            }
        });
    }

    @Override
    protected void initData(Context mContext) {
        tvStoreName.setText(DataUtil.getInstance().getStoreName());
    }

    @Override
    protected void loadMethod() {
        isSetCompany();
        getCardNo();
        getResult();
    }

    @OnClick({R.id.tv_card_suf})
    public void onViewClicked(View view) {
        if (ThrottleFirstUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_card_suf:
                String userName = etUserName.getText().toString().trim();
                String userPhone = etUserPhone.getText().toString().trim();
                String psdOne = etPsdOne.getText().toString().trim();
                String psdTwo = etPsdTwo.getText().toString().trim();
                memberShipCardMode.checkMember(getActivity(), userName, userPhone, psdOne, psdTwo, paySwitch, productId, carNumber, getArguments().getString("uuid"), cardKindId);
                break;

        }
    }

    private void isSetCompany() {
        if (StringUtils.isEmpty(DataUtil.getInstance().getStoreId())) {
            ToastUtils.showToast(getContext().getApplicationContext(), "暂未设置集团门店，请联系运营人员进行修改！");
            getActivity().finish();
        }
    }

    private void getCardNo() {
        memberShipCardMode.getLiveData().observe(this, bean -> {
            if (bean == null) {
                ToastUtils.showToast(getContext().getApplicationContext(), "数据返回异常!!");
                return;
            }
            oilCardAdapter.setListData(bean.getCardKindDTOs());
            oilCardAdapter.notifyDataSetChanged();
            carNumber = bean.getCarNumber();
            tvCard.setText(bean.getCarNumber() + "");
        });
        memberShipCardMode.getCardNo(getActivity(), getArguments().getString("uuid"));
    }

    private void getResult() {
        memberShipCardMode.getliveDataResult().observe(this, integer -> {
            hideInput();
            if (integer == 1) {
                startWithPop(OpenCardSuccessFragment.newInstance(getArguments().getString("uuid")));
            } else if (integer == 2) {
                startWithPop(CardReplaceFragment.newInstance(etUserPhone.getText().toString().trim(), "1"));
            }

        });
    }

    /**
     * 关闭软键盘
     */
    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(INPUT_METHOD_SERVICE);
        View v = getActivity().getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }
}
